package cn.openea.perpartner.crm.web.mapper;

import cn.openea.perpartner.crm.web.entity.CrmMenu;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import icu.mhb.mybatisplus.plugln.base.mapper.JoinBaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 菜单权限表(CrmMenu)表数据库访问层
 *
 * @author Weird077
 * @since 2022-12-19 22:30:36
 */
@Repository
public interface CrmMenuMapper extends JoinBaseMapper<CrmMenu> {

    @Select("select menu_id, menu_name, parent_id, order_num, path, component, `query`, is_frame, is_cache, menu_type, visible, status, ifnull(perms,'') as perms, icon, create_time " +
            "from crm_menu ${ew.customSqlSegment}")
    List<CrmMenu> selectMenuList(@Param("ew") QueryWrapper<CrmMenu> wrapper);

    @Select("select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time" +
            " from crm_menu m" +
            " left join crm_role_menu rm on m.menu_id = rm.menu_id" +
            " left join crm_user_role ur on rm.role_id = ur.role_id" +
            " left join crm_role ro on ur.role_id = ro.role_id ${ew.customSqlSegment}")
    List<CrmMenu> selectMenuListByUserId(@Param("ew") QueryWrapper<CrmMenu> wrapper);

    @Select("select distinct m.perms" +
            " from crm_menu m" +
            " left join crm_role_menu rm on m.menu_id = rm.menu_id" +
            " left join crm_user_role ur on rm.role_id = ur.role_id" +
            " left join crm_role r on r.role_id = ur.role_id" +
            " where m.status = '0' and r.status = '0' and ur.user_id = #{userId}")
    List<String> selectMenuPermsByUserId(String userId);

    @Select("select distinct m.perms" +
            " from crm_menu m" +
            " left join crm_role_menu rm on m.menu_id = rm.menu_id" +
            " where m.status = '0' and rm.role_id = #{roleId}")
    List<String> selectMenuPermsByRoleId(Long roleId);

    @Select("select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time" +
            " from crm_menu m where m.menu_type in ('M', 'C') and m.status = 0" +
            " order by m.parent_id, m.order_num")
    List<CrmMenu> selectMenuTreeAll();

    @Select("select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time" +
            " from crm_menu m" +
            " left join crm_role_menu rm on m.menu_id = rm.menu_id" +
            " left join crm_user_role ur on rm.role_id = ur.role_id" +
            " left join crm_role ro on ur.role_id = ro.role_id" +
            " left join crm_user u on ur.user_id = u.user_id" +
            " where u.user_id = #{userId} and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0" +
            " order by m.parent_id, m.order_num")
    List<CrmMenu> selectMenuTreeByUserId(String userId);
    @Select("select m.menu_id from crm_menu m left join crm_role_menu rm on m.menu_id = rm.menu_id ${ew.customSqlSegment}")
    List<Long> selectMenuListByRoleId(@Param("ew") QueryWrapper wrapper);

}

